package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.domain.vo.NearUserVo;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;


@DubboService
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //上报地理位置信息
    @Override
    public void uploadLocation(Long userId, String longitude, String latitude, String addrStr) {
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if (userLocation == null){
             userLocation = new UserLocation();
             userLocation.setUserId(userId);
             userLocation.setLocation(new GeoJsonPoint(Double.parseDouble(longitude),Double.parseDouble(latitude)));
             userLocation.setAddress(addrStr);
             userLocation.setCreated(System.currentTimeMillis());
             userLocation.setUpdated(System.currentTimeMillis());
             userLocation.setLastUpdated(System.currentTimeMillis());

             mongoTemplate.save(userLocation);
        }else {
            Update update = new Update();
            update.set("lastUpdated",userLocation.getUpdated());
            update.set("updated",System.currentTimeMillis());
            mongoTemplate.updateFirst(query,update ,UserLocation.class );
        }
    }

    //查询范围内用户信息
    @Override
    public List<Long> searchNearUserId(Long userId , Double distance) {
        //根据userId查询位置信息
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //1、确定圆心
        GeoJsonPoint location = userLocation.getLocation();

        //2、确定半径
        Distance dis = new Distance(distance/1000 , Metrics.KILOMETERS);

        //3、以中心为位置画个指定半径的圆
        Circle circle = new Circle(location, dis);

        //4、在圆范围内查找
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);

        //返回neatUserVo的集合，里面存着userId
        ArrayList<Long> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocationList)) {
            for (UserLocation userLocationByList : userLocationList) {
                Long userID = userLocationByList.getUserId();
                vos.add(userID);
            }
        }

        return vos;
    }
}
